package org.panda.ibog;

import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.InputStream;

public class ResourceUtils {
    public static final String winFileSeperator = "\\";
    public static final String linuxFileSeperator = "/";

    /**
     * Read inputstream from classpath resource
     * @param fileName
     * @return
     */
    public static InputStream load(String fileName){
        InputStream is = null;
        if (!StringUtils.isBlank(fileName)){
            if (fileName.indexOf(winFileSeperator)==0 || fileName.indexOf(linuxFileSeperator)==0){
                fileName = fileName.substring(1);
            }
            return  ResourceUtils.class.getClassLoader().getResourceAsStream(fileName);
        }
        return is;
    }

    public static InputStream load(String fileName, Class clazz){
        InputStream is = null;
        if (clazz == null){
            return load(fileName);
        }
        if (!StringUtils.isBlank(fileName)){
            if (fileName.indexOf(winFileSeperator)==0 || fileName.indexOf(linuxFileSeperator)==0){
                is = load(fileName);
            }
            InputStream is2 = clazz.getResourceAsStream(fileName);
            if(is2 != null){
                is = is2;
            }
        }
        return is;
    }

}
